<?php
namespace app\task\controllers;

use app\task\models\TaskModel;
use lib\redis\RedisTool;
use lib\rule\ParentInit;

class MyController extends ParentInit{

    public function actionIndex(){
        $redis = RedisTool::_init(0);
        $counumber = 0;
        while (true){
            $data = $redis->setKey('TASK_LIST')->getListRight()->getReturn();
            if (empty($data)){
                sleep(5);
                $counumber++;
                if ($counumber >= 1440){
                    exit();
                }
                continue;
            }
            $data = json_decode($data,true);
            echo $data['task_frequency'].'<--->';
            $data['task_frequency']--;
            $task_src= str_replace(',','\\',$data['task_src']);
            sleep($data['task_delay']);
            $obj = new $task_src();
            $res = $obj->execute(json_decode($data['task_param'],true));
            if ($data['task_frequency'] >= 0){
                if ($data['task_frequency'] > 0){
                    $redis->setKey('TASK_LIST')->setListLeft(json_encode($data));
                }
                $up_task = md5($data['task_name'].$data['task_src']);
                TaskModel::find()->where([['=','task_md5',$up_task],['!=','task_frequency',0]],'and')->update(['task_result'=>$res,'task_frequency'=>$data['task_frequency']]);
            }
            echo $data['task_frequency'].PHP_EOL;
        }
    }
}